#include "main.h"
//**********************************************************返回本机IP**********************************************************
uint32_t getip(void)
{
    struct ifaddrs *ifAddrStruct = NULL;
	struct ifaddrs *ifa = NULL;
	void *tmpAddrPtr = NULL;

	getifaddrs(&ifAddrStruct);

	for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) 
	{
		if (ifa ->ifa_addr->sa_family == AF_INET) 
		{ 
			tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
			char addressBuffer[INET_ADDRSTRLEN];
			inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
			if (strcmp(ifa->ifa_name, "lo") != 0)
			{ 
				printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
                		return ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
            		}
		}
	}
	if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
}

//引用锁资源
extern pthread_mutex_t pm1,pm2;
//**********************************************************创建**********************************************************
//创建账户信息链
struct user_link* create_user_link(void)
{
	int ret,len=0;
	struct user_link* P=malloc(sizeof(struct user_link));
	if(P==NULL){printf("创建账户信息链失败\n");return NULL;}
	P->tail=P;P->next=NULL;
	FILE*F=fopen("login.txt","r");
	if(F==NULL){perror("打开账户信息文件失败");return NULL;}
	struct user test_user={0};
	while(1)
	{
		memset(&test_user,0,sizeof(test_user));
		if(fscanf(F,"账号:%20s 密码:%20s \n",test_user.name,test_user.password)==EOF)break;
		struct user_link* P1=malloc(sizeof(struct user_link));
		strcpy(P1->userID.name,test_user.name);
		strcpy(P1->userID.password,test_user.password);
		P->tail->next=P1;
		P->tail=P1;
		P1->next=NULL;
		len++;
	}
	
		struct user_link* tp=P->next;
		for(int i=0;i<len;i++)
		{
			printf("读取到的第%d个账号:%20s,密码:%20s\n",i,tp->userID.name,tp->userID.password);
			tp=tp->next;
		}
	
	fclose(F);
	printf("读取创建账户链成功\n");
	return P;
}

//创建在线客户端链
struct client_link* create_client_link(void)
{
	int ret,len=0;
	struct client_link *P=malloc(sizeof(struct client_link));
	if(P==NULL){printf("创建在线客户端链失败\n");return NULL;}
	memset(P,0,sizeof(struct client_link));
	P->tail=P;P->next=NULL;
	printf("创建客户端链成功\n");
	return P;
}

//**********************************************************查找**********************************************************
//查找账户链
int find_user(struct user_link*P,struct user user_date)//0:账户密码正确 -1:密码错误  -2:账户不存在
{
	pthread_mutex_lock(&pm1);
	struct user_link* test=P->next;
	while(test!=NULL)
	{
		if(strcmp(test->userID.name,user_date.name)==0)
		{
			pthread_mutex_unlock(&pm1);
			if(strcmp(test->userID.password,user_date.password)==0)
			{
				printf("查询账户链<%s>账号密码正确\n",user_date.name);
				return 0;
			}
			else
			{
				printf("查询账户链<%s>密码错误\n",user_date.name);
				return -1;
			}
		}
		test=test->next;
	}
	pthread_mutex_unlock(&pm1);
	printf("查询账户链<%s>账号不存在\n",user_date.name);
	return -2;
}

//查找在线客户端链
int find_client(struct client_link*P,struct user user_date)//0:账号存在  -1:账号不存在
{
	pthread_mutex_lock(&pm2);
	struct client_link* test=P;
	while(test!=NULL)
	{
		if(strcmp(test->name,user_date.name)==0) 
		{
			pthread_mutex_unlock(&pm2);
			printf("查询在线链<%s>账号已存在\n",user_date.name);
			return 0;
		}
		test=test->next;
	}
	pthread_mutex_unlock(&pm2);
	printf("查询在线链<%s>账号不在线\n",user_date.name);
	return -1;
}

//**********************************************************修改**********************************************************
//修改账户链
int user_link_ctl(struct user_link*P,int type,struct user user_date)//操作账户信息链链 1:添加 2:删除
{
	int ret=-1;
	pthread_mutex_lock(&pm1);
	if(type==1)
	{
		struct user_link*P1=malloc(sizeof(struct user_link));
		if(P1==NULL)
			ret=-1;
		{
			strcpy(P1->userID.name,user_date.name);
			strcpy(P1->userID.password,user_date.password);
			P->tail->next=P1;
			P->tail=P1;
			P1->next=NULL;
			ret=0;
			//添加至文件
			FILE*F=fopen("login.txt","a");
			if(F==NULL)
				perror("账户添加至文件失败\n");
			else
				fprintf(F,"账号:%20s 密码:%20s \n",user_date.name,user_date.password);
			fclose(F);
			//*************************************用户文件************************************************
			////创建账户名文件夹(联系人文件，群聊列表文件)
			char test1[]="mkdir user_data/",test2[]="touch user_data/",test3[]="/",test4[]=".txt";
			char test[100]={0};
			//创建用户文件夹
			sprintf(test,"%s%s",test1,user_date.name);
			printf("\n%s\n",test);
			system(test);
			//创建用户联系人列表
			sprintf(test,"%s%s/%s.txt",test2,user_date.name,user_date.name);
			printf("\n%s\n",test);
			system(test);
		}
	}
	else if(type==2)
	{
		struct user_link*test=P;
		while(test->next!=NULL)
		{
			if(strcmp(test->next->userID.name,user_date.name)==0)
			{
				struct user_link*ttest=test->next;
				if(ttest==P->tail)P->tail=test;
				test->next=ttest->next;
				free(ttest);
				ttest=NULL;
				ret=0;
				printf("账户:%s注销成功\n",user_date.name);
			}
			test=test->next;
		}
	}
	pthread_mutex_unlock(&pm1);
	return ret;
}

//修改在线客户端链
int client_link_ctl(struct client_link*P,int type,struct sockaddr_in addr,char*name)//操作在线客户端链 1:添加 2:删除
{
	int ret=-1;
	pthread_mutex_lock(&pm2);
	if(type==1)
	{
		struct client_link*P1=malloc(sizeof(struct client_link));
		if(P1==NULL)
			ret=-1;
		else
		{
			P1->addr=addr;
			strcpy(P1->name,name);
			P1->TP=time(NULL);
			P->tail->next=P1;
			P->tail=P1;
			P1->next=NULL;
			ret=0;
			//printf("在线客户端<%s-%s>添加成功\n",inet_ntoa(addr.sin_addr),name);
		}
	}
	else if(type==2)
	{
		struct client_link*test=P;
		while(test->next!=NULL)
		{
			if(test->next->addr.sin_addr.s_addr==addr.sin_addr.s_addr)
			{
				struct client_link*ttest=test->next;
				if(ttest==P->tail)P->tail=test;
				test->next=ttest->next;
				free(ttest);
				ttest=NULL;
				ret=0;
				printf("在线客户端<%s-%s>删除成功\n",inet_ntoa(addr.sin_addr),name);
			}

		}
	}
	pthread_mutex_unlock(&pm2);
	return ret;
}
